$(document).ready(function() {
    /**
     * I'd like to give all my "widgets" but since jQuery got a datepicker
     * which gives jquery-ui classess I'm not able to change this.
     * Maybe some day (when I have enough time left) I will write my own 
     * datepicker widget but untill then the classess are not: dmf-widget e.t.c.
     */
    $widget_datepicker.loadDates();
        
    //Remove unnecessary javascript files (since there already loaded)
    $(".dmf-dependencies script").remove();
});

var $widget_datepicker = {
    loadDates : function() {
        $(".dmf-widget-datepicker").each(function() {
            $widget_datepicker.load( this );
        });
    },
    configureDatepicker : function() {
        $("body").find(".ui-datepicker-calendar td").each(function() {
            $(this).mouseover(function() {
                $(this).removeClass("dmf-datepicker-highlight").addClass("dmf-datepicker-highlight");
            });

            $(this).click(function() {
                $(this).find("a").trigger("click");
            });

            $(this).mouseout(function() {
                $(this).removeClass("dmf-datepicker-highlight");
            });
        });
        
        $(".ui-datepicker-prev").mouseup(function() {
            setTimeout(function() {
                
                $widget_datepicker.configureDatepicker("left");
            }, 100);
        });

        $(".ui-datepicker-next").mouseup(function() {
            setTimeout(function() {
                $widget_datepicker.configureDatepicker("right");
            }, 100);
        });
    },
    load : function( date ) {
        $(date).find("input").datepicker();
        
        $(date).find("input").first().focus(function() {
            $widget_datepicker.configureDatepicker();
        });
    }
};